package cn.befory.tools;

import cn.befory.constants.CommonConstants;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * 分页工具类
 * Created by befory on 2019/1/12.
 */
public class PageUtil<T> {
    private int pageNo;

    private int pageSize;

    private int total;

    private int totalPage;

    private List<T> list;

    private PageUtil(){
        this(1, CommonConstants.DEFAULT_SIZE,0,1, Collections.emptyList() );
    }

    private PageUtil(int pageNo, int pageSize, int total, int totalPage, List<T> list){
        this.pageNo = pageNo;
        this.pageSize = pageSize;
        this.total = total;
        this.totalPage = totalPage;
        this.list = list;
    }

    public static PageUtil createPage(int pageNo,int pageSize,int total,List<?> list){
        if(pageNo < CommonConstants.ONE){
            pageNo = CommonConstants.ONE;
        }
        if(pageSize < CommonConstants.DEFAULT_SIZE){
            pageSize = CommonConstants.DEFAULT_SIZE;
        }
        if(pageSize > CommonConstants.MAX_PAGE_SIZE){
            pageSize = CommonConstants.MAX_PAGE_SIZE;
        }
        int totalPage = getTotalPage(total,pageSize);
        if(list == null){
            list =  new ArrayList<>();
        }
        return new PageUtil(pageNo,pageSize,total,totalPage,list);
    }

    public int getPageNo() {
        return pageNo;
    }

    public void setPageNo(int pageNo) {
        this.pageNo = pageNo;
    }

    public int getPageSize() {
        return pageSize;
    }

    public void setPageSize(int pageSize) {
        if(pageSize < CommonConstants.DEFAULT_SIZE){
            pageSize = CommonConstants.DEFAULT_SIZE;
        }
        if(pageSize > CommonConstants.MAX_PAGE_SIZE){
            pageSize = CommonConstants.MAX_PAGE_SIZE;
        }
        this.pageSize = pageSize;
    }

    public int getTotal() {
        return total;
    }

    public void setTotal(int total) {
        if(total < 0){
            total = 0;
        }
        this.total = total;
    }

    private static int getTotalPage(int total,int pageSize){
        int totalPage = total % pageSize == 0 ? total / pageSize:(total / pageSize +1);
        if(totalPage < CommonConstants.ONE){
            totalPage = CommonConstants.ONE;
        }
        return totalPage;
    }
    public void setTotalPage() {
        this.totalPage = getTotalPage(this.total,this.pageSize);;
    }

    public int getTotalPage(){
        return totalPage;
    }

    public List<T> getList() {
        return list;
    }

    public PageUtil setList(List<T> list) {
        if(list == null){
            list = new ArrayList<T>();
        }
        this.list = list;
        return this;
    }

    public static int getOffset(int pageNo, int pageSize){
        if(pageNo < CommonConstants.ONE){
            pageNo = CommonConstants.ONE;
        }
        if(pageSize < CommonConstants.DEFAULT_SIZE){
            pageSize = CommonConstants.DEFAULT_SIZE;
        }
        return (pageNo -CommonConstants.ONE) * pageSize;
    }
}
